package com.flyme.common.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.util.Md5Utils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Md5Sign {
    /**
     * MD5生成签名字符串
     *
     * @param map 需签名参数
     * @param key MD5key
     * @return
     */
    public static String sign(Map<String, Object> map, String key) {
        String genSign = "";
        try {

            String[] signFields = new String[4];
            signFields[0] = "secretkey";
            signFields[1] = "userid";
            signFields[2] = "secretkey";
            signFields[3] = "ts";
            JSONObject param = (JSONObject) JSONObject.toJSON(map);
            // 生成签名原文
            String signSrc = orgSignSrc(signFields, param);
            // MD5的方式签名
            //signSrc += "&KEY=" + key;
            genSign = Md5Utils.getMD5(signSrc.getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return genSign;
    }

    /**
     * 构建签名原文
     *
     * @param param 参数与值的jsonbject
     * @return
     */
    private static String orgSignSrc(String[] signFields, JSONObject param) {
        if (signFields != null) {
            Arrays.sort(signFields); // 对key按照 字典顺序排序
        }

        StringBuffer signSrc = new StringBuffer("");
        int i = 0;
        for (String field : signFields) {
            signSrc.append(field);
            signSrc.append("=");
            signSrc.append((StringUtils.isEmpty(param.getString(field)) ? ""
                    : param.getString(field)));
            // 最后一个元素后面不加&
            if (i < (signFields.length - 1)) {
                signSrc.append("&");
            }
            i++;
        }
        return signSrc.toString();
    }

    /**
     * MD5验证签名
     *
     * @param map
     * @param key
     * @param sign
     * @return
     */
    public static void vlidateMD5sign(Map<String, Object> map, String key, String sign) {
        String vsign = sign(map, key);
        System.out.println("MD5验证签名生成的签名：" + vsign);
        System.out.println("MD5验证签名生成的签名与原签名是否一致：" + (vsign.equals(sign)));
    }

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("secretkey", "mfoS918bQZ");
        map.put("userid", 12456874);
        map.put("secretkey", "mfoS918bQZ");
        map.put("ts", "1498547407000");
        /***MD5签名与验签**/
        String key = "123456ADSEF";
        String sign = sign(map, key);
        System.out.println("生成的MD5签名：" + sign);
        Long ts = System.currentTimeMillis();
        System.out.println("时间戳"+ts);
        String s=sign("12456874",ts.toString());
        System.out.println("生成的MD5签名33：" + s);
        getUrl(33333L,302114);
        vlidateMD5sign(map, key, sign);
    }



    public static String sign(String userid,String ts) {
        String signSource = "CIlI3K1WEq"+userid+"CIlI3K1WEq"+ts;
        String sign = Md5Utils.getMD5(signSource.getBytes());
        return sign;
    }


    public static String getUrl(Long userId,Integer chanleId) {
        Long ts = System.currentTimeMillis();
        String signSource = "CIlI3K1WEq"+userId+"CIlI3K1WEq"+ts;
        String sign = Md5Utils.getMD5(signSource.getBytes());
        String url="https://live.polyv.cn/watch/"+chanleId+"?userid="+userId+"&ts="+ts+"&sign="+sign;
        System.out.println(url);
        return url;
    }


}
